package com.wenhuitec.android.tang.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.dhkj.web.bean.ProductModel;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.eventb.FoodsClickEvent;


import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FoodsCardAdapter extends RecyclerView.Adapter<FoodsCardAdapter.MyViewHolder> {

    private LayoutInflater inflater;

    private Context mContext;

    private List<ProductModel.Device> datas = new ArrayList<>();

    private List<ProductModel.Device> cartBeanList;

    public FoodsCardAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    public FoodsCardAdapter(Context context, List<ProductModel.Device> addSkuModelList) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        this.datas = addSkuModelList;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_foods_card, parent, false);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.foodsNameTv.setText(datas.get(position).getDevicename());
        holder.priceTv.setText(datas.get(position).getSale_price());

        holder.foodsCardRv.setOnClickListener(view -> {
            FoodsClickEvent foodsClickEvent = new FoodsClickEvent();
            foodsClickEvent.setDevice(datas.get(position));
            EventBus.getDefault().post(foodsClickEvent);
        });
//        cartBeanList = GlobalBean.getInstance().getCartBeanList();
//        if (cartBeanList != null) {
//            List<ProductModel> productModelList = cartBeanList.stream()
//                    .filter(productModel -> productModel.getProduct().getProductId()
//                            .equals(datas.get(position).getProduct().getProductId())).collect(Collectors.toList());
//
//            int count = 0;
//            for (ProductModel goo : productModelList) {
//                count += goo.getCartNum();
//            }
//            if (productModelList.size() > 0) {
//                holder.tipsRv.setVisibility(View.VISIBLE);
//                holder.cartNumTv.setText(String.valueOf(count));
//            } else {
//                holder.tipsRv.setVisibility(View.GONE);
//            }
//        }


        holder.kuTv.setText("库：" + datas.get(position).getNumber().stripTrailingZeros().toPlainString());
        holder.gkTv.setText("规格：" + datas.get(position).getGk().trim());
    }


    public void setDatas(List<ProductModel.Device> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView foodsNameTv;
        TextView priceTv;
        RelativeLayout foodsCardRv;

        RelativeLayout tipsRv;
        TextView cartNumTv;
        TextView kuTv;
        TextView gkTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            foodsNameTv = itemView.findViewById(R.id.foodsNameTv);
            priceTv = itemView.findViewById(R.id.priceTv);
            foodsCardRv = itemView.findViewById(R.id.foodsCardRv);

            tipsRv = itemView.findViewById(R.id.tipsRv);
            cartNumTv = itemView.findViewById(R.id.cartNumTv);
            kuTv = itemView.findViewById(R.id.kuTv);
            gkTv = itemView.findViewById(R.id.gkTv);
        }
    }
}
